﻿$cp.common = {
    post: function (args) {
        $.ajax({
            url: args.url,
            data: JSON.stringify(args.data),
            processData: false,
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            type: 'POST',
            success: args.success,
            error: args.failure
        });
    },
    makeRelative: function (path) {
        return $cp.relativePath + path;
    },
    blockPage: function (msg) {
        $.blockUI({
            css: {
                border: 'none',
                padding: '15px',
                backgroundColor: '#000',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                opacity: .5,
                color: '#fff'
            },
            message: msg
        });
    },
    blockPartial: function (element, msg) {
        $(element).block({
            css: {
                border: 'none',
                padding: '12px',
                'font-size': '14px',
                backgroundColor: '#000',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                opacity: .5,
                color: '#fff'
            },
            message: msg
        });
    },
    unblock: function (element) {
        if (element == null) {
            $core.unblockUI();
        }
        else {
            $(element).unblock();
        }
    },
    recaptcha: {
        load: function () {
            if (!$cp.debugMode && !$cp.localhost) {
                Recaptcha.create("6Le5zsYSAAAAAGoAWbupHZxnGpG2NXUlKkAnKw8N",
                    "recaptcha-panel",
                    {
                        theme: "clean",
                        callback: null
                    });
            }
        },
        reload: function () {
            if (!$cp.debugMode && !$cp.localhost) {
                Recaptcha.reload();
            }
        },
        destroy: function () {
            if (!$cp.debugMode && !$cp.localhost) {
                Recaptcha.destroy();
            }
        }
    },
    validEmail: function (input) {
        var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        return pattern.test(input);
    },
    validPhone: function (input) {
        var pattern = /^(?:\+?[0-9]*[-. ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
        return pattern.test(input);
    },
    validate: function (form) {
        var isValid = true;
        var requiredInputFields = $(form).find('input.required');
        var requiredSelectFields = $(form).find('select.required');
        var emailFields = $(form).find('input.email');
        var phoneFields = $(form).find('input.phone');
        requiredInputFields.removeClass('ui-state-error');
        requiredInputFields.each(function () {
            if ($(this).val().length == 0) {
                $(this).addClass('ui-state-error').focus();
                isValid = false;
                return;
            }
        });
        requiredSelectFields.each(function () {
            if ($(this).val().length == 0) {
                $(this).addClass('ui-state-error').focus();
                isValid = false;
                return;
            }
        });
        emailFields.each(function () {
            if (!$core.validEmail($(this).val())) {
                $(this).addClass('ui-state-error').focus();
                isValid = false;
                return;
            }
        });
        phoneFields.each(function () {
            if (!$core.validPhone($(this).val())) {
                $(this).addClass('ui-state-error').focus();
                isValid = false;
                return;
            }
        });
        return isValid;
    }
}
var $core = $cp.common;